var  currentFrame = 'homeFrame';
upCountLog();
/**
 * 跳转主页
 * created by dale 2017/10/20
 */
function goHome() {
    $.mobile.go('#home','slide','right');
}

function setCurrentFrame(frameName) {
    currentFrame = frameName;
}

/**
 * 跳转到目标面板
 *
 * created by dale 20171129
 */
function goTarget() {
    stopSlider();
    $.mobile.go($('#targetPanel'));
}


function stopSlider() {

    if(currentFrame == 'homeFrame'||currentFrame == 'shortcutFrame'){
        window.top.frames[currentFrame].silder.data('flexslider').pause();
        try{
            chageColor('#0066cc');
        }catch (e){

        }
    }
}
function startSlider() {
    if(currentFrame == 'homeFrame'||currentFrame == 'shortcutFrame'){
        frames[currentFrame].silder.data('flexslider').play();
        // setTimeout(function () {
        //     $('.flexslider',frames[currentFrame].document).click();
        //     alert('click');
        // },1000);

        try{
            chageColor(window.top.frames[currentFrame].silder.data("flexslider").find('.flex-active-slide').find('img').attr('stuscolor'));
        }catch (e){

        }
    }
}



/**
 * 错误信息
 * @param $msg
 * @param callback
 *
 * create by dale 20171216
 */
function sendErrorMsg($msg,callback) {
    swal({title:'错误',text: $msg,type:'error',confirmButtonText:'确定'}).then(function () {
        if(callback){
            callback.call(this);
        }
    },function () {
       return false
    });
}

/**
 * 询问
 * @param $title
 * @param $msg
 * @param callback
 *
 * create by dale 20180705
 */
function askDelAction($title,$msg,callback) {
    window.top.swal({
        title: $title ? $title : '警告',
        text:  $msg ? $msg : '请确认要删除这条记录嘛？',
        type: 'info',
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消'
    }).then(
        function () {
            if(callback){
                callback.call(this);
            }
        }, function () {
            return false
        });
}


function goBack() {
    $('#back').click();
}


/**
 * 成功信息
 * @param $msg
 * @param callback
 *
 * create by dale 20171216
 */
function sendSuccessMsg($msg,callback) {
    swal({title:'成功',text: $msg,type:'success',confirmButtonText:'确定'}).then(function () {
        if(callback){
            callback.call(this);
        }
    },function () {
        return false
    });
}


/**
 * 警告信息
 * @param $msg
 * @param success
 * @param error
 *
 * create by dale 20171219
 */
function sendWarn($msg,success,error) {
    swal({title:'警告',text: $msg,type:'warning',confirmButtonText:'确定',cancelButtonText:'取消'}).then(function () {
        if(success){
            success.call(this);
        }
    },function () {
        if(error){
            error.call(this);
        }
    });
}

/**
 * 跳转下一个目标网页
 * @param options {url:'',title:''}
 *
 * create by dale 20171219
 */
function openNextTarget(options) {
    var _default={
        'title':'',
        'url':baseUrl+'/mobile'
    };
    options.url = baseUrl+options.url;
    options=$.extend({}, _default, options);

    $('#targetFrame').attr('src',options.url);
    setTitle(options.title);
    goTarget();
}

/**
 * 修改标题
 * @param title
 * created by dale 2017/10/20
 */
function setTitle(title) {
    $('#targetPanel-title').html(title);
}


$(document).ready(function () {
    hideOverlay();
    // $('.tabs-selected a').click(function () {
    //
    // });
    upCountLog();
    $('#homeTabs').tabs({
        onSelect:function (title,index) {
            var str = '';
            switch (index){
                case  0 : str='home';break;
                case  1 : str='backlog';break;
                case  2 : str='shortcut';break;
                case  3 : str='mine';break;
            }
            if(window.top.frames[str+'Frame'].location.href == 'about:blank'){
                window.top.frames[str+'Frame'].location.href = baseUrl+'mobile/index/'+str;
            }else {
                if(index === 0){
                    $.ajax({
                        url:baseUrl+'mobile/index/ajaxCheckUpdate',
                        method:'post',
                        data:{maps:window.top.frames[str+'Frame'].homeMap,module:1},
                        dataType:'json',
                        success:function (data) {
                            if(data['flag']){
                                location.reload();
                            }
                        }
                    });
                }
                if(index === 1){
                    $.ajax({
                        url:baseUrl+'mobile/index/ajaxCheckUpdate',
                        method:'post',
                        data:{maps:window.top.frames[str+'Frame'].backLogMaps,module:2},
                        dataType:'json',
                        success:function (data) {
                            if(data['flag']){
                                window.top.frames[str+'Frame'].location.reload();
                                upCountLog();
                            }
                        }
                    });
                }
            }
            stopSlider();
            setCurrentFrame(str+'Frame');
            startSlider();

        }
    });
    bindBack(function () {
        goHome();
    });
});

/**
 * 绑定返回方法
 * @param func
 * 
 * created by dale 20171129 
 */
function bindBack(func) {
    $('#back').unbind('click').click(function () {
        func();
        startSlider();
    });
}

/**
 * 打开下一个页面时，注册好返回的事件
 *
 * created by dale 20171129
 */
function registerBack() {
    var title = $('.m-title').text();
    var href  = window.frames['targetFrame'].location.href;
    bindBack(function () {
        $('#targetFrame').attr('src',href);
        setTitle(title);
    })
}

/**
 * 打开扫描
 * create by dale 20180122
 */
function openScan() {
    var flag = identifyDev();
    if(flag == 1){
        window.XueHai.openScanf();
    }else if(flag == 2){
        window.webkit.messageHandlers.openScanf.postMessage(null);
    }else {
        // sendErrorMsg('找不到设备类型');
    }
}

/**
 * 改变颜色
 * create by dale 20180122
 */
function chageColor(color) {
    var flag = identifyDev();
    if(flag == 1){
        window.XueHai.setBarColor(color);
    }else if(flag == 2){
        window.webkit.messageHandlers.setBarColor.postMessage({color:color});
    }else {
        // sendErrorMsg('找不到设备类型');
    }
}

/**
 * 获取uuId
 *
 * create by dale 20180809
 */
function getUUID() {
    var flag = identifyDev();
    if(flag == 1){
        window.XueHai.getUUID();
    }else if(flag == 2){
        window.webkit.messageHandlers.getUUID.postMessage(null);
    }else {
        // sendErrorMsg('找不到设备类型');
    }
}


/**
 * 判断android ios 设备
 * create by dale 20180122
 */
function identifyDev() {
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    // console.log(isAndroid);
    // console.log(isiOS);
    if(isAndroid){
        return 1;
    }else if(isiOS) {
        return  2;
    }else {
        return 3;
    }
}


/**
 * 获取待办记录数目
 *
 * created by dale 2017/11/29
 */
function upCountLog() {
    $.ajax({
        url:baseUrl+'mobile/backlog/getLogCount',
        type:'POST',
        dataType:'json',
        success:function (data) {
            $('#logCount').html(data['count']);
        },
        error:function (data) {
            console.log(data);
        }
    });
}

/**
 * 扫码回调
 * @type {{setResult: JSObjectHandle.setResult}}
 * created by dale 2017/9/15
 */
var JSObjectHandle = {
    setResult : function (code) {
        if(currentFrame == 'mineFrame' ){
            window.frames['mineFrame'].JSObjectHandle.setResult(code);//支持mineFrame扫码回调
        }else {
            window.frames['targetFrame'].JSObjectHandle.setResult(code);
        }
    },
    setMacAddress :function (address) {
        window.frames['targetFrame'].JSObjectHandle.setMacAddress(address);
    },
    setUUID:function (uuid) {

        window.frames[currentFrame].JSObjectHandle.setUUID(uuid);
    }
}




// $(function () {
//     hideOverlay();
// });


/* 显示遮罩层 */
function showOverlay() {
    var overlay = $("#overlay");
    overlay.height(pageHeight());
    overlay.width(pageWidth());

    // fadeTo第一个参数为速度，第二个为透明度
    // 多重方式控制透明度，保证兼容性，但也带来修改麻烦的问题
    overlay.fadeTo(200, 0.5);
}

/* 隐藏覆盖层 */
function hideOverlay() {
    $("#overlay").fadeOut(200);
}

/* 当前页面高度 */
function pageHeight() {
    return document.body.scrollHeight;
}

/* 当前页面宽度 */
function pageWidth() {
    return document.body.scrollWidth;
}

